package com.yycx.module.bus.provider.handler.haiying.tasktodo.monitor;

import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.service.BaseAdminUserService;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单查询审核监控
 *
 * @author zyf
 */
@Component
public class HyOrderTaskMonitorListHandler implements PageInterceptor<HyOrder> {

    @Resource
    private BaseUserService userService;

    @Autowired
    private SysCompanyService companyService;


    @Autowired
    private FlowableHelp flowableHelp;

    @Override
    public void prepare(CriteriaQuery<HyOrder> cq, PageParams pageParams, EntityMap params) {
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String customId = cq.getParams("customId");
        String orderNo = cq.getParams("orderNo");
        Long companyId = OpenHelper.getCompanyId();
        cq.eq(HyOrder.class, "deptId");
        cq.eq(HyOrder.class, "companyId");
        cq.like(HyOrder.class, "saleManName");
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge("sendDate", beginDate);
            cq.le("sendDate", endDate);
        }
        cq.eq(FlymeUtils.isNotEmpty(customId), "customId", customId);
        cq.in(true, "orderState", 1, 2);
        cq.eq(true, "orderType", HySaleTypeEnum.CHA.getCode());
        cq.eq(HyOrder.class, "companyId", companyId);
        cq.orderByDesc("createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            String assignee = entityMap.get("assignee");
            EntityMap taskMap = flowableHelp.getTaskInfo(processInstanceId);
            if (FlymeUtils.isNotEmpty(taskMap)) {
                entityMap.putAll(taskMap);
            }
            Integer orderState = entityMap.getInt("orderState");
            Long companyId = entityMap.getLong("companyId");
            if (FlymeUtils.isNotEmpty(companyId)) {
                SysCompany company = companyService.getById(companyId);
                entityMap.put("companyName", company.getCompanyName());
            }
            BaseUser orderUser = userService.getById(assignee);
            if (FlymeUtils.isNotEmpty(orderUser)) {
                entityMap.put("nickName", orderUser.getNickName());
            }
            if (orderState.equals(2)) {
                entityMap.put("taskName", "已审批");
            }

        }
        return result;
    }

}
